package com.demo.priority_queue;

import com.demo.commons.message.PriorityQueueMessage;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component;

/**
 * @author shenguangyang
 */
@Component
//@RabbitListener(queues = "${rabbitmq.queues.priorityQueue.name}")
@RabbitListener(bindings = {
        @QueueBinding(value = @Queue(value = "mq.priority.queue", arguments = {
                @Argument(name = "x-max-priority", value ="100", type = "java.lang.Long")
        }),
        exchange = @Exchange(value = "mq.direct.test", type = ExchangeTypes.DIRECT))
})
public class PriorityQueueListener {
    @RabbitHandler
    public void onMessage(PriorityQueueMessage msg, Channel channel, Message message) {
        try {
            System.out.println(msg);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e) {
            try {
                channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,true);
                // channel.basicRecover(true);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }
}
